package cn.slf.ph.servlet;

import cn.slf.ph.dao.CommunityDao;
import cn.slf.ph.dao.impl.CommunityDaoImpl;
import cn.slf.ph.entity.Community;
import cn.slf.ph.entity.Shop;
import cn.slf.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 读取action并判断
        String action = req.getParameter("action");
        CommunityDao communityDao = new CommunityDaoImpl();
        // 根据action进行执行不同的代码
        if ("selectAll".equals(action)){
            // 接受数据
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String communityName = req.getParameter("communityName");
            // 封装到map集合
            Map<String,String> map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("communityName",communityName);
            // 连接数据库
            // 查询所有的数据
            List<Community> list = communityDao.selectAll(map);
            // 查询数据总条数
            int total = communityDao.selectCount(map);
            // 给出响应信息
            //需要显示出 List：list[]  total: num  这两个键值对   所以用一个map 键值对 来放入data部分
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            //显示为json格式
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("update".equals(action)){
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null && !"".equals(id)){
                communityId = Integer.parseInt(id);
            }
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            String communityCity = req.getParameter("communityCity");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityAddress = req.getParameter("communityAddress");
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String addOrUpdateFlag = req.getParameter("addOrUpdateFlag");
            String ca = req.getParameter("communityAreaCode");
            int communityAreaCode = 0;
            if (ca!=null && !"".equals(ca)){
                communityAreaCode = Integer.parseInt(ca);
            }
            String communityCoordinates = req.getParameter("communityCoordinates");
            // 组成对象
            Community community = new Community(communityId,  communityName,  communityProvince,  communityCity,  communityDistrict,  communityAddress,  communityLatitude,  communityLongitude,communityCoordinates, communityAreaCode);
            if ("1".equals(addOrUpdateFlag)){
                int num = communityDao.insert(community);
                Result result = null;
                if (num!=0){
                    result = Result.success("新增成功");
                }else {
                    result = Result.error("新增失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                int num = communityDao.update(community);
                Result result = null;
                if (num!=0){
                    result = Result.success("修改成功");
                }else {
                    result = Result.error("修改失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if ("delete".equals(action)){
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null && !"".equals(id)){
                communityId = Integer.parseInt(id);
            }
            int num = communityDao.delete(communityId);
            Result result = null;
            if (num!=0){
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("associatedStores".equals(action) || "findShopListByAdd".equals(action)){
            // 接受数据
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            String communityId = req.getParameter("communityId");
            // 封装到map集合
            Map<String,String> map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("shopName",shopName);
            map.put("communityId",communityId);
            List<Shop> list = null;
            int total = 0;
            if ("associatedStores".equals(action)){
                // 连接数据库
                // 查询所有的数据
                list = communityDao.associatedStores(map);
                // 查询数据总条数
                total = communityDao.associatedStoresCount(map);
                // 给出相应信息
            }else {
                list = communityDao.findShopListByAdd(map);
                total = communityDao.findShopListByAddCount(map);
            }

            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("addAssociatedStores".equals(action)){
            String id = req.getParameter("fendeShopCommunityList");
            String [] sid =id.split(",");
            String communityId = req.getParameter("panghuCommunityId");
            int panghuCommunityId = Integer.parseInt(communityId);
            int num = 0;
            for (String ID : sid){
                int shopId = Integer.parseInt(ID);
                num = communityDao.addAssociatedStores(panghuCommunityId, shopId);
            }
            Result result = null;
            if (num!=0){
                result = Result.success("操作成功");
            }else {
                result = Result.error("操作失败");
            }
            communityDao.associatedupdate(panghuCommunityId);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("associatedStoresRemove".equals(action)){
            String id = req.getParameter("panghuShopId");
            int panghuShopId = Integer.parseInt(id);
            int num = communityDao.associatedStoresRemove(panghuShopId);
            Result result = null;
            if (num!=0){
                result = Result.success("操作成功");
            }else {
                result = Result.error("操作失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
